import { radioChecked, searchCover, deepCopy, isString, isNumber, isArray } from "@/common.js"
// import { post } from "@/utils/network.js"
// 调整字段值
export const changeField = function (e, field) {
  e = e || ''
  const value = isString(e) || isNumber(e) || isArray(e) ? e : e.target.value
  // 更新右侧的值
  const item = {...this.item, [field]: value}
  this.$emit('setItem', item)
  // 更新整体表单的值
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [field]: value}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [field]: value})) : v.children}))
  this.$emit('setFormData', formData)
}
// 获取单选的选项列表
export const getOptions = async function () {
  // if(!this.item.optionsUrl) { return false }
  // const options = await post(this.item.optionsUrl, {})
  // const formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, options: options}))
  // this.$emit('setFormData', formData)
}
// 改变下拉选项
export const changeSelect = function (e) {
  const value2 = (this.item.options.at(-1) || {})['id'] != e ? '' : this.item.value2
  this.$emit('setItem', {...this.item, value1: e, value2, errmsg: '' })
  const item = this.item.options.find(v => v.id == e) || {}
  const {link = '' , type = '' } = item
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, value1: e, value2, errmsg: ''}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, value1: e, value2, errmsg: ''})) : v.children}))
  formData = searchCover(formData, v => link.split(',').includes(v.key), v => ({...v, disabled: type == 1}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, v => link.split(',').includes(v.key), v => ({...v, disabled: type == 1})) : v.children}))
  this.$emit('setFormData', formData)
  eval(this.item.onChange)(e)
}
// 改变第二个选项的值
export const changeValue2 = function (e) {
  const value2 = isString(e) || isNumber(e) || isArray(e) ? e : e.target.value
  // 目前代码只实现了value2的配置
  this.changeField(value2, 'value2')
}